package com.mobiusws.reports.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mobiusws.reports.dao.DailyCellQualityDao;
import com.mobiusws.reports.dao.DailyFraudSummaryDao;
import com.mobiusws.reports.dao.TestDao;
import com.mobiusws.reports.model.DailyCellQuality;
import com.mobiusws.reports.model.DailyFraudSummary;
import com.mobiusws.reports.model.LoginInfo;
import com.mobiusws.util.ExportUtil;
import com.mobiusws.util.JsonUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DailyCellQualityAction extends ActionSupport{

	private static final long serialVersionUID = 5395964004991248282L;

	@Resource
	private DailyCellQualityDao dailyCellQualityDao;
	@Resource
	private JsonUtil jsonUtil;
	@Resource
	private ExportUtil exportUtil;
	private String filePrefix="daily_cell_quality_report";
	private String filename;
	private String exportSqlId="getDailyFraudSummaryAllExport";

	private String result;

	private String selDate;
	private int start;
	private int limit;
	private int total;

	public String getSelDate() {
		return selDate;
	}

	public void setSelDate(String selDate) {
		this.selDate = selDate;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	// a getter is required to get the result in pages
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	@Override
	public String execute() throws Exception {
		// Thread.sleep(1000);
//		System.out.printf("selDate=%s, start=%s, limit=%s\n",
//				this.getSelDate(), this.getStart(), this.getLimit());
		List<DailyCellQuality> list = dailyCellQualityDao
				.getDailyCellQuality(this);
		this.result = jsonUtil.getJsonByList(list, this.getTotal());
		// System.out.println(this.result);
		ServletActionContext.getResponse().setContentType(
				"text/plain;charset=utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(this.result);
		out.flush();
		out.close();
		return SUCCESS;
	}
	
//	public String exportCSV() throws Exception {
//		String shortFilename=filePrefix+"_"+selDate+".csv";
//		this.filename=exportUtil.exportCSV(shortFilename, exportSqlId, new Object[]{selDate});
//		return "export";
//	}
//	
//	public String exportExcel() throws Exception {
//		String datetime=selDate.replaceAll("[^0-9]", "");
//		String shortFilename=filePrefix+"_"+datetime+".xlsx";
//		//long startTime=System.currentTimeMillis();
//		this.filename=exportUtil.exportExcel(shortFilename, exportSqlId, new Object[]{selDate});
//		//long timeConsumed=System.currentTimeMillis()-startTime;
//		//System.out.println("It took "+((double)timeConsumed/1000) + "(s) to generate export file " + shortFilename);
//		return "export";
//	}

}
